package com.appspot.battlerafts.servlets.user;

import com.appspot.battlerafts.classes.User;
import com.appspot.battlerafts.jsons.GameDataJSON;
import com.appspot.battlerafts.utils.PMF;
import com.google.gson.Gson;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.appspot.battlerafts.utils.Util.returnSuccess;

/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 02.04.12
 * Time: 14:22
 * To change this template use File | Settings | File Templates.
 */
public class CreateOrMarkOnlineUser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new Gson();
        GameDataJSON gameDataJSON = gson.fromJson(request.getParameter("data"), GameDataJSON.class);

        PersistenceManager pm = PMF.get().getPersistenceManager();
        User user;
        try {
            user = (User) pm.getObjectById(User.class, gameDataJSON.playerUID);
            user.setOnline(true);
        } catch (JDOObjectNotFoundException e) {
            user = new User(gameDataJSON.playerUID);
            pm.makePersistent(user);
        } finally {
            pm.close();
        }

        returnSuccess(response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
